package com.itaomiao.filter;

import java.io.IOException;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

/**
 * Servlet Filter implementation class ServletFilter
 */
//@WebFilter("/servletFilter")
public class ServletFilter implements Filter {

    @Override
    public void destroy() {
        System.out.println("ServletFilter destroy…");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        response.setCharacterEncoding("utf-8") ;
        HttpServletRequest  request2=(HttpServletRequest)request;
       String requestUrl = request2.getScheme() //当前链接使用的协议
               +"://" + request2.getServerName()//服务器地址 
               + ":" + request2.getServerPort() //端口号 
               + request2.getContextPath() //应用名称，如果应用名称为
               + request2.getServletPath() //请求的相对url 
               + "?" + request2.getQueryString(); //请求参数
       System.out.println(new Date().toString() +" : "+ requestUrl);
       filterChain.doFilter(request2, response);
      
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        System.out.println("ServletFilter init…");
    }

}
